package mylab.utils.common.jackson;

import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.cfg.CacheProvider;
import com.fasterxml.jackson.databind.ser.DefaultSerializerProvider;
import com.fasterxml.jackson.databind.ser.SerializerFactory;

public class CustomSerializerProvider extends DefaultSerializerProvider {


    public CustomSerializerProvider() {
        super();
    }

    public CustomSerializerProvider(SerializerProvider src, SerializationConfig config, SerializerFactory f) {
        super(src, config, f);
    }

    @Override
    public DefaultSerializerProvider withCaches(CacheProvider cacheProvider) {
        return new DefaultSerializerProvider.Impl();
    }

    @Override
    public DefaultSerializerProvider createInstance(SerializationConfig config, SerializerFactory jsf) {
        return new CustomSerializerProvider(this, config, jsf);
    }

    @Override
    public JsonSerializer<Object> findNullValueSerializer(BeanProperty property) throws JsonMappingException {
        JavaType type = property.getType();

        if (type.getRawClass().equals(String.class)) {
            return new CustomJsonSerializer.NullStringJsonSerializer();
        }
        if (type.getRawClass().equals(Boolean.class)) {
            return new CustomJsonSerializer.NullBooleanJsonSerializer();
        }
        if (type.getRawClass().equals(Number.class)) {
            return new CustomJsonSerializer.NullNumberJsonSerializer();
        }
        if (type.isArrayType() || type.isCollectionLikeType()) {
            return new CustomJsonSerializer.NullArrayJsonSerializer();
        }

        return super.findNullValueSerializer(property);
    }
}
